package com.xiguaji.tiktok.v1.tiktok.mytiktok.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.spider.BloggerSpiderClient;
import com.xiguaji.tiktok.spider.GetBloggerResponse;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.BaseController;
import com.xiguaji.tiktok.v1.tiktok.aweme.model.Aweme;
import com.xiguaji.tiktok.v1.tiktok.aweme.service.AwemeService;
import com.xiguaji.tiktok.v1.tiktok.blogger.dto.SearchData;
import com.xiguaji.tiktok.v1.tiktok.blogger.model.Blogger;
import com.xiguaji.tiktok.v1.tiktok.blogger.service.BloggerService;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.dto.*;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.model.TBlogger;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.model.TGroup;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.model.TGroupBlogger;
import com.xiguaji.tiktok.v1.tiktok.mytiktok.service.MyTiktokService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.user.log.Operation;
import com.xiguaji.tiktok.v1.user.log.ParamsType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@Slf4j
@Api(value = "My Tiktok",tags = {"My Tiktok"})
public class MyTiktokController extends BaseController {

    @Autowired
    MyTiktokService myTiktokService;

    @Autowired
    BloggerService bloggerService;

    @Autowired
    AwemeService awemeService;

    @Autowired
    BloggerSpiderClient bloggerSpiderClient;

    @Autowired
    Blogger.BloggerMapper bloggerMapper;

    /*########################################################分组  start########################################################*/
    @ApiOperation(value = "添加分组")
    @RequestMapping(value = "my/tiktok/group",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @Operation(value = "添加分组")
    public String addGroup(@RequestBody String body) throws ParamsException {
        String groupName = getParam(body,"groupName").toString().trim();
        if(myTiktokService.checkGroupName(getUser().getId(),null,groupName)){
            return Ret.err("The name exists already!");
        }
        TGroup tGroup = new TGroup();
        tGroup.setUserId(getUser().getId());
        tGroup.setName(groupName);
        tGroup.setGroupSort(System.currentTimeMillis()/1000);
        tGroup.setCreateBy("add Group");
        tGroup.setUpdateBy("add Group");
        myTiktokService.addOrUpdateGroup(tGroup);
        return Ret.success("Group added successfully!");
    }

    @ApiOperation(value = "更新分组名称")
    @RequestMapping(value = "my/tiktok/group/rename",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    @Operation(value = "更新分组名称")
    public String renameGroup(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        String groupName = getParam(body,"groupName").toString().trim();

        if(myTiktokService.checkGroupName(getUser().getId(),groupId,groupName)){
            return Ret.err("The name exists already!");
        }
        TGroup tGroup = myTiktokService.findGroupById(groupId);
        tGroup.setName(groupName);
        tGroup.setUpdateBy("update Group Name");
        myTiktokService.addOrUpdateGroup(tGroup);
        return Ret.success("OK");
    }

    @ApiOperation(value = "删除分组")
    @RequestMapping(value = "my/tiktok/group",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
    @Operation(value = "删除分组")
    public String delGroup(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        myTiktokService.deleteGroup(groupId);
        return Ret.success("Deleted sucessfully！");
    }

    @ApiOperation(value = "删除博主")
    @RequestMapping(value = "my/tiktok/group/blogger",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
    @Operation(value = "删除博主")
    public String delBlogger(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        long uid = getParam(Long.class,body,"uid");

        if(groupId == 0){
            myTiktokService.deleteBlogger(getUser().getId(),uid);
        }else{
            myTiktokService.deleteGroupBlogger(groupId,uid);
        }
        return Ret.success("Deleted sucessfully！");
    }

    @ApiOperation(value = "置顶分组")
    @RequestMapping(value = "my/tiktok/group/top",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    @Operation(value = "置顶分组")
    public String topGroup(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        TGroup tGroup = myTiktokService.findGroupById(groupId);
        tGroup.setGroupSort(System.currentTimeMillis()/1000);
        tGroup.setUpdateBy("update group sort");
        myTiktokService.addOrUpdateGroup(tGroup);
        return Ret.success("Group pinned successfully!");
    }

    @ApiOperation(value = "置顶分组下的博主")
    @RequestMapping(value = "my/tiktok/blogger/top",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    @Operation(value = "置顶分组下的博主")
    public String topBloggerByGroup(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        long uid = getParam(Long.class,body,"uid");

        if(groupId == 0) {
            var blogger = myTiktokService.getBlogger(getUser().getId(), uid);
            blogger.setBloggerSort(System.currentTimeMillis()/1000);
            blogger.setUpdateBy("update blogger top");
            myTiktokService.saveBlogger(blogger);
            return Ret.success("TikToker pinned successfully!");
        }

        var groupBlogger = myTiktokService.getGroupBlogger(groupId, uid);
        groupBlogger.setBloggerSort(System.currentTimeMillis()/1000);
        groupBlogger.setUpdateBy("update blogger top");
        myTiktokService.saveGroupBlogger(groupBlogger);
        return Ret.success("TikToker pinned successfully!");
    }

    @ApiOperation(value = "移动博主分类")
    @RequestMapping(value = "my/tiktok/blogger/move",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    @Operation(value = "移动博主分类")
    public String moveBloggerToGroup(@RequestBody String body) throws ParamsException {
        long groupId = getParam(Long.class,body,"groupId");
        long uid = getParam(Long.class,body,"uid");
        TGroupBlogger tGroupBlogger = myTiktokService.getGroupBloggerByUserIdAndUid(getUser().getId(),uid);

        if(null == tGroupBlogger){
            tGroupBlogger = new TGroupBlogger();
            tGroupBlogger.setUid(getUser().getId());
            tGroupBlogger.setGroupId(groupId);
            tGroupBlogger.setUid(uid);
            tGroupBlogger.setUserId(getUser().getId());
            tGroupBlogger.setBloggerSort(System.currentTimeMillis()/1000);
            tGroupBlogger.setCreateBy("add move Group");
            tGroupBlogger.setUpdateBy("add move Group");
            myTiktokService.saveGroupBlogger(tGroupBlogger);
            return Ret.success("Moved to the group successfully!");
        }

        if(tGroupBlogger.getGroupId() == groupId){
            return Ret.err("The TikToker has already been assigned to the group.");
        }
        tGroupBlogger.setGroupId(groupId);
        tGroupBlogger.setUpdateBy("move Group");
        tGroupBlogger.setBloggerSort(System.currentTimeMillis()/1000);
        myTiktokService.saveGroupBlogger(tGroupBlogger);
        return Ret.success("Moved to the group successfully!");
    }

    @ApiOperation(value = "获取分组数据")
    @RequestMapping(value = "my/tiktok/group",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取分组数据")
    public String getGroupData(){
        return Ret.success("OK",myTiktokService.getGroupDataList(getUser().getId()));
    }
    /*########################################################分组  end########################################################*/

    /*########################################################分组 博主列表 start########################################################*/
    @ApiOperation(value = "分组中博主列表(增长数据)")
    @RequestMapping(value = "my/tiktok/group/blogger/{groupId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "分组中博主列表")
    public String getBloggerListByGroup(@PathVariable long groupId){
        val bloggerListByGroup = myTiktokService.getBloggerListByGroup(groupId, getUser().getId());
        return Ret.pageData(bloggerListByGroup.size(),bloggerListByGroup);
    }

    @ApiOperation(value = "导出分组中博主列表(增长数据)")
    @RequestMapping(value = "my/tiktok/group/blogger/export",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "导出分组中博主列表")
    public void exportBloggerListByGroup(String data, HttpServletResponse response) throws IOException, ParamsException {
        ExcelWriter writer = ExcelUtil.getWriter();
        long groupId = getParam(Long.class, URLDecoder.decode(data,"UTF-8"),"groupId");
        String [] columns = new String[]{"Name","Username","Posts-Today","Posts-Yesterday","Follower Growth-Today","Follower Growth-Yesterday","View Growth-Today","View Growth-Yesterday","Likes Growth-Today","Likes Growth-Yesterday","Review Growth-Yesterday","Review Growth-Yesterday","Share Growth-Today","Share Growth-Yesterday"};
        val bloggerListByGroup = myTiktokService.getBloggerListByGroup(groupId, getUser().getId());

        List<Map<String,Object>> rows = new ArrayList<>();
        bloggerListByGroup.forEach( bloggerDataResponse -> {
            if(bloggerDataResponse.getUid() == 0){
                return;
            }
            Map<String,Object> row = new LinkedHashMap<>();
            row.put(columns[0],bloggerDataResponse.getNickname());
            row.put(columns[1],bloggerDataResponse.getUniqueId());
            row.put(columns[2],bloggerDataResponse.getTodayData().getPostsGrowth());
            row.put(columns[3],bloggerDataResponse.getYesterdayData().getPostsGrowth());
            row.put(columns[4],bloggerDataResponse.getTodayData().getFollowerGrowth());
            row.put(columns[5],bloggerDataResponse.getYesterdayData().getFollowerGrowth());
            row.put(columns[6],bloggerDataResponse.getTodayData().getViewGrowth());
            row.put(columns[7],bloggerDataResponse.getYesterdayData().getViewGrowth());
            row.put(columns[8],bloggerDataResponse.getTodayData().getLikeGrowth());
            row.put(columns[9],bloggerDataResponse.getYesterdayData().getLikeGrowth());
            row.put(columns[10],bloggerDataResponse.getTodayData().getReviewGrowth());
            row.put(columns[11],bloggerDataResponse.getYesterdayData().getReviewGrowth());
            row.put(columns[12],bloggerDataResponse.getTodayData().getShareGrowth());
            row.put(columns[13],bloggerDataResponse.getYesterdayData().getShareGrowth());
            rows.add(row);
        });
        Map<String,Object> countRow = new LinkedHashMap<>();
        countRow.put(columns[0],"Total");
        countRow.put(columns[1],"");
        countRow.put(columns[2],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getPostsGrowth());
        countRow.put(columns[3],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getPostsGrowth());
        countRow.put(columns[4],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getFollowerGrowth());
        countRow.put(columns[5],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getFollowerGrowth());
        countRow.put(columns[6],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getViewGrowth());
        countRow.put(columns[7],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getViewGrowth());
        countRow.put(columns[8],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getLikeGrowth());
        countRow.put(columns[9],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getLikeGrowth());
        countRow.put(columns[10],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getReviewGrowth());
        countRow.put(columns[11],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getReviewGrowth());
        countRow.put(columns[12],bloggerListByGroup.get(bloggerListByGroup.size()-1).getTodayData().getShareGrowth());
        countRow.put(columns[13],bloggerListByGroup.get(bloggerListByGroup.size()-1).getYesterdayData().getShareGrowth());
        rows.add(countRow);


        writer.merge(rows.size(),rows.size(),0,1,"ttt",true);
        writer.write(rows,true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+Integer.parseInt(DateUtil.format(LocalDateTime.now(),"yyyyMMdd"))+".xls");
        writer.flush(response.getOutputStream());
        writer.close();
    }

    /**
     * 获取组下的播主列表信息及统计信息(总数据)
     * @param groupId
     * @return
     */
    @ApiOperation(value = "获取组下的播主列表信息及统计信息(总数据)")
    @RequestMapping(value = "my/tiktok/group/blogger/total/{groupId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取组下的播主列表信息及统计信息(总数据)")
    public String getBloggerTotalListByGroup(@PathVariable long groupId){
        val bloggerListByGroup = myTiktokService.getBloggerTotalListByGroup(groupId, getUser().getId());
        return Ret.pageData(bloggerListByGroup.size(),bloggerListByGroup);
    }

    /**
     * 导出组下的播主列表信息及统计信息(总数据)
     * @param data
     * @param response
     * @throws IOException
     * @throws ParamsException
     */
    @ApiOperation(value = "导出组下的播主列表信息及统计信息(总数据)")
    @RequestMapping(value = "my/tiktok/group/blogger/total/export",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "导出组下的播主列表信息及统计信息(总数据)")
    public void exportBloggerTotalListByGroup(String data, HttpServletResponse response) throws IOException, ParamsException {
        ExcelWriter writer = ExcelUtil.getWriter();
        //解析参数
        long groupId = getParam(Long.class, URLDecoder.decode(data,"UTF-8"),"groupId");
        //导出的数据集
        List<Map<String,Object>> rows = myTiktokService.getExportBloggerTotalListData(groupId, getUser().getId());
        //设置导出excel的参数,合并统计行的开头
        writer.merge(rows.size(),rows.size(),0,1,"",true);
        //设置数据
        writer.write(rows,true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = "My_TikTokers_" + DateUtil.format(LocalDateTime.now(),"yyyyMMdd") + ".xls";
        response.setHeader("Content-Disposition","attachment;filename=" + fileName);
        writer.flush(response.getOutputStream());
        writer.close();
    }
    /*########################################################分组 博主列表 end########################################################*/





    /*########################################################详情 start########################################################*/

    @ApiOperation(value = "获取博主信息")
    @RequestMapping(value = "my/tiktok/blogger/{uid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取博主信息",fetchParam = "uid",paramType = ParamsType.URL)
     public String getBloggerDetail(@PathVariable long uid){
        return Ret.success("OK",bloggerService.getBloggerInfo(uid,getUser().getId()));
    }

    @ApiOperation(value = "获取博主最近记录时间")
    @RequestMapping(value = "my/tiktok/blogger/record/time",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取博主最近记录时间")
     public String getBloggerLastRecordTime(){
        return Ret.success("OK",myTiktokService.getLastBloggerRecordTime());
    }


    @ApiOperation(value = "获取播主走势数据")
    @RequestMapping(value = "my/tiktok/blogger/data/{uid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取播主走势数据",fetchParam = "uid",paramType = ParamsType.URL)
    public String getDayData(@PathVariable long uid){
        int hourType = 4;
        long roleId = getRoleId();

        switch ((int) roleId){
            case 1:
                break;
            case 2:
                break;
            case 3:
                hourType = 2;
                break;
            case 4:
                hourType = 1;
                break;
        }
        return Ret.success("OK",myTiktokService.getDayData(uid,hourType));
    }

    @ApiOperation(value = "博主视频列表")
    @RequestMapping(value = "my/tiktok/blogger/aweme/{uid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "博主视频列表",fetchParam = "uid",paramType = ParamsType.URL)
    public String getBloggerAweme(@PathVariable long uid,String data) throws UnsupportedEncodingException, ParamsException {
        SearchData searchData = getParam(SearchData.class, URLDecoder.decode(data,"UTF-8"));
        val awemePageByBlogger = bloggerService.getAwemePageByBlogger(uid, searchData,getUser().getId());
        return Ret.pageData(awemePageByBlogger.getTotalCount(),awemePageByBlogger.getList());
    }

    @ApiOperation(value = "博主视频列表导出")
    @RequestMapping(value = "my/tiktok/blogger/aweme/export",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "博主视频列表")
    public void exportBloggerAweme(String data, HttpServletResponse response) throws IOException, ParamsException {
        int infoType = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"),"infoType");
        int sort = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"),"sort");
        long uid = getParam(Long.class,URLDecoder.decode(data,"UTF-8"),"uid");
        SearchData searchData = new SearchData();
        searchData.setInfoType(infoType);
        searchData.setSort(sort);
        searchData.setPage(1);
        searchData.setPageSize(1000);
        val blogger = bloggerService.getBloggerByUid(uid);
        val awemePageByBlogger = bloggerService.getAwemePageByBlogger(uid, searchData,getUser().getId());
        String [] columns = new String[]{"Spread Index","Description","Views","Likes","Reviews","Share","View Growth-Today","Duration","Upload"};
        List<Map<String,Object>> rows = new ArrayList<>();
        awemePageByBlogger.getList().forEach( a->{
            Map<String,Object> row = new LinkedHashMap<>();
            row.put(columns[0],a.getScore());
            row.put(columns[1],a.getTitle());
            row.put(columns[2],a.getViews());
            row.put(columns[3],a.getLikes());
            row.put(columns[4],a.getReviews());
            row.put(columns[5],a.getShares());
            row.put(columns[6],a.getDuration());
            row.put(columns[7],a.getCreateTime());
            rows.add(row);
        });
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.write(rows,true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+Integer.parseInt(DateUtil.format(LocalDateTime.now(),"yyyyMMdd"))+blogger.getOUniqueId()+".xls");
        writer.flush(response.getOutputStream());
        writer.close();
    }
    /*########################################################详情 end########################################################*/

    /*########################################################视频详情 end########################################################*/
    @ApiOperation(value = "视频详情")
    @RequestMapping(value = "my/tiktok/aweme/{awemeId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "视频详情",fetchParam = "awemeId",paramType = ParamsType.URL)
    public String getBloggerAweme(@PathVariable long awemeId){
        Aweme aweme = awemeService.findByAwemeId(awemeId);
        val bloggerData = myTiktokService.getBlogger(getUser().getId(), aweme.getOUid());

        if(bloggerData.getCreateTime() > aweme.getOCreateTime()){
            return Ret.AwemeErr("Video heat monitoring is only available for videos posted after the account being added to My TikToker");
        }

        int hourType = 4;
        long roleId = getRoleId();

        switch ((int) roleId){
            case 1:
                break;
            case 2:
                break;
            case 3:
                hourType = 2;
                break;
            case 4:
                hourType = 1;
                break;
        }
        AwemeDataResponse awemeDataResponse = new AwemeDataResponse();
        awemeDataResponse.setCover(aweme.getOCover());
        awemeDataResponse.setAwemeId(aweme.getOAwemeId());
        awemeDataResponse.setCreateTime(aweme.getOCreateTime());
        awemeDataResponse.setDuration(aweme.getODuration());
        awemeDataResponse.setScore(aweme.getScore());
        awemeDataResponse.setTitle(aweme.getODesc());
        awemeDataResponse.setShareUrl(aweme.getOShareUrl());

        val awemeData = myTiktokService.getAwemeData(awemeId, hourType);
        awemeDataResponse.setGrowthData(awemeData);
        return Ret.success("OK",awemeDataResponse);
    }
    /*########################################################视频详情 end########################################################*/

    /*########################################################添加博主 start########################################################*/

    @ApiOperation(value = "搜索博主",notes = "%7B%22infoType%22%3A1%2C%22sort%22%3A1%2C%22pageSize%22%3A10%2C%22page%22%3A1%7D")
    @RequestMapping(value = "my/tiktok/search",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "搜索博主")
    public String searchTiktok(String data) throws UnsupportedEncodingException, ParamsException {
        String keyword = getParam(URLDecoder.decode(data,"UTF-8"),"keyword");
        List<SearchDataResponse> searchDataResponses = new ArrayList<>();

        try{
            val discover = bloggerSpiderClient.discover(keyword);
            discover.getUser_list().forEach( user_list -> {
                Blogger blogger = bloggerMapper.mapper(user_list.getUser_info());
                SearchDataResponse searchDataResponse = new SearchDataResponse();
                searchDataResponse.setAvatar(blogger.getOAvatar168());
                searchDataResponse.setCustomVerify(blogger.getOEnterpriseVerifyReason());
                searchDataResponse.setEnterpriseVerifyReason(blogger.getOEnterpriseVerifyReason());
                searchDataResponse.setNickname(blogger.getONickname());
                searchDataResponse.setRegion(blogger.getORegion());
                searchDataResponse.setUid(blogger.getOUid());
                searchDataResponse.setUniqueId(blogger.getOUniqueId());
                TBlogger b = myTiktokService.getBlogger(getUser().getId(),blogger.getOUid());
                if(null != b){
                    searchDataResponse.setAdded(true);
                }
                searchDataResponses.add(searchDataResponse);
            });
            return Ret.success("OK",searchDataResponses);
        }catch (Exception e){
            log.error("search error ",e);
            return Ret.success("OK",myTiktokService.searchBloggerByES(keyword,getUser().getId()));
        }
    }

    @ApiOperation(value = "添加监控博主")
    @RequestMapping(value = "my/tiktok/blogger",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @RequestLimit(value = "AddMyTiktokLimit",validateType = Limit.ADD_MY_TIKTOK_LIMIT)
    @Operation(value = "添加监控博主")
    public String addTiktok(@RequestBody String body) throws ParamsException {
        MTBloggerResponse bloggerResponse = getParam(MTBloggerResponse.class,body);
        TBlogger tBlogger = myTiktokService.getBlogger(getUser().getId(),bloggerResponse.getUid());

        if(null != tBlogger){
            return Ret.err("The Blogger exists already!");
        }
        tBlogger = new TBlogger();
        tBlogger.setUid(bloggerResponse.getUid());
        tBlogger.setUserId(getUser().getId());
        tBlogger.setAvatar(bloggerResponse.getAvatar());
        tBlogger.setCustomVerify(bloggerResponse.getCustomVerify());
        tBlogger.setEnterpriseVerifyReason(bloggerResponse.getEnterpriseVerifyReason());
        tBlogger.setNickname(bloggerResponse.getNickname());
        tBlogger.setUniqueId(bloggerResponse.getUniqueId());
        tBlogger.setRegion(bloggerResponse.getRegion());
        tBlogger.setBloggerSort(System.currentTimeMillis()/1000);
        tBlogger.setCreateBy("add blogger");
        tBlogger.setUpdateBy("add blogger");
        myTiktokService.saveBlogger(tBlogger);
        // 判断是否收录过该博主


        Blogger blogger = bloggerService.getBloggerByUid(tBlogger.getUid());

        if(null == blogger){
            GetBloggerResponse response = bloggerSpiderClient.get(tBlogger.getUid(), tBlogger.getRegion());
            blogger = bloggerMapper.mapper(response.getUser());
            blogger.setLastCollectTime(new Date());
            blogger.setBloggerType("");
            blogger.setWithFusionShopEntry(false);
            blogger.setLastPubTime(new Date());
            blogger.setScore(0.0);
            blogger.setUpdateBy("mytiktok add blogger");
            blogger.setCreateBy("mytiktok add blogger");
            bloggerService.save(blogger);
        }
        return Ret.success("System will automatically start collecting this TikTok account.Some data will be shown in 24 hours.");
    }

    /*########################################################添加博主 end########################################################*/



}
